package com.tsingteng.cosfun.widget.dialog.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.VideoSectionBean;
import com.tsingteng.cosfun.utils.DesityUtils;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.StringUtils;

import java.util.List;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/10/21
 * qq(872179966)
 */

public class VideoSliceTextAdapter extends BaseQuickAdapter<VideoSectionBean, BaseViewHolder> {
    private int with = 0;

    public VideoSliceTextAdapter() {
        super(R.layout.item_video_slice_text_adapter);
    }

    @Override
    protected void convert(BaseViewHolder helper, VideoSectionBean item) {
        TextView textView = helper.getView(R.id.tv_slice_video);
        RelativeLayout rlSliceRoot = helper.getView(R.id.rl_slice_root);
        TextView textViewRight = helper.getView(R.id.tv_slice_video_right);
        List<VideoSectionBean> data = getData();
        int sectionEndPoint = data.get( helper.getAdapterPosition()).getSectionEndPoint();
        long sectionStartPoint = data.get( helper.getAdapterPosition()).getSectionStartPoint();
        int time = item.getSectionVideoSpan();
        int all = 0;
        for (int i = 0; i < data.size(); i++) {
            all = all + data.get(i).getSectionVideoSpan();
        }
        if ((all / 1000)==0)return;
        int t = with / (all / 1000);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) rlSliceRoot.getLayoutParams();
        if (item.isSelect()) {
            rlSliceRoot.setVisibility(View.VISIBLE);
            int textWith;
            if (t * time / 1000 < 140) {
                if (sectionStartPoint >= 10000) {
                    textWith = 140;
                } else {
                    if (sectionEndPoint >= 10000) {
                        textWith = 130;
                    } else {
                        textWith = 120;
                    }
                }
                LogUtils.e(TAG,"textWith1"+textWith+"posi"+  helper.getAdapterPosition());
                layoutParams.width =  RecyclerView.LayoutParams.WRAP_CONTENT;
                if ( helper.getAdapterPosition() != 0 &&  helper.getAdapterPosition() != data.size() - 1) {
                    layoutParams.setMargins(-((textWith - (t * time / 1000)) / 2), 0, ((textWith - (t * time / 1000)) / 2), 0);
                    LogUtils.e(TAG,"adapterPosition != 0 && adapterPosition != data.size() - 1"+((textWith - (t * time / 1000)) / 2));
                } else if ( helper.getAdapterPosition() == data.size() - 1) {
                    layoutParams.setMargins(-((textWith - (t * time / 1000))), 0,0, 0);
                    LogUtils.e(TAG,"adapterPosition == data.size() - 1"+((textWith - (t * time / 1000)) / 2));
                }else {
                    layoutParams.setMargins(0, 0, ((textWith - (t * time / 1000))), 0);
                    LogUtils.e(TAG,"adapterPosition == 0"+((textWith - (t * time / 1000)) / 2));
                }
            } else {
                layoutParams.width = t * time / 1000;
                LogUtils.e(TAG,"textWith2"+(t * time / 1000)+"pos"+  helper.getAdapterPosition());
            }
            rlSliceRoot.setVisibility(View.VISIBLE);
            textViewRight.setText(StringUtils.div(sectionEndPoint, 1000, 1) + "s");
            textView.setText(StringUtils.div(sectionStartPoint, 1000, 1) + "s");
        } else {
            layoutParams.width = t * time / 1000;
            rlSliceRoot.setVisibility(View.INVISIBLE);
            LogUtils.e(TAG,"textWith3"+(t * time / 1000)+"pos"+  helper.getAdapterPosition());
        }
        rlSliceRoot.setLayoutParams(layoutParams);
    }

    public void setWith(int with) {
        this.with = with;
    }


    public int dp2px(float px) {
        return  DesityUtils.dp2px(px);
    }
}
